package com.yhyvr.api.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.lz.kit.PropKit;
import com.tencent.common.Sign;
import com.yhyvr.api.handle.ApiData;
import com.yhyvr.api.service.wx.WxService;
import com.yhyvr.api.tool.ViewFactory;
import com.yhyvr.pojo.ApiConstant;
import com.yhyvr.pojo.req.api.ApiReqBody;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.req.api.store.order.ApiUnifiedorderReq;
import com.yhyvr.pojo.req.api.wx.WxJsapiReq;
import com.yhyvr.pojo.resp.HttpResult;
import com.yhyvr.pojo.resp.api.ApiRespData;
import com.yhyvr.pojo.resp.api.wx.WxJsapiResp;
import com.yhyvr.service.ProxyServiceI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by - on 2016/11/17.
 * 微信api
 */
@Controller
public class WxController{

    @Resource
    private WxService wxService;

    @RequestMapping("/api/store/wx/jspai")
    public @ResponseBody
    ModelAndView jsapi(@ApiData(tokenValidate = false) ApiReqData<WxJsapiReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(wxService.jsApi(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }



    @RequestMapping("/api/test")
    public String jsapi(ModelMap modelMap,HttpServletRequest request){
        WxJsapiResp wxJsapiResp = new WxJsapiResp();
        String ticket = wxService.getTicket();
        if(ticket!=null) {
            JSONObject map = Sign.sign(ticket, request.getRequestURL().toString());
            if (map != null) {
                wxJsapiResp.setAppId(PropKit.use("three-app.properties").get("wx.appid"));
                wxJsapiResp.setJsapiTicket(map.getString("jsapi_ticket"));
                wxJsapiResp.setUrl(map.getString("url"));
                wxJsapiResp.setNonceStr(map.getString("nonceStr"));
                wxJsapiResp.setTimestamp(map.getString("timestamp"));
                wxJsapiResp.setSignature(map.getString("signature"));
                modelMap.put("o",wxJsapiResp);
            }
        }
        return "test";
    }
}
